# -*- coding: utf8 -*-
from urllib.parse import urlparse,urlencode,unquote
from http.server import BaseHTTPRequestHandler,HTTPServer
import json
from beidou_code_point import beidou_grid_code_point


class GetHandler(BaseHTTPRequestHandler):
    def do_GET(self):
        parsed_path = urlparse(self.path)
        query = unquote(parsed_path.query)
        path = parsed_path.path
        if 'point' in path:
            query_segment = query.split('&')
            query_dict = dict(seg.split('=') for seg in query_segment)
            # print(query_dict)
            longitude = query_dict.get('longitude')
            latitude = query_dict.get('latitude')
            level_n = int(query_dict.get('level'))
            result = beidou_grid_code_point(longitude, latitude, level_n)
        elif 'line' in path:
            pass
        elif 'polygon' in path:
            pass
        else:
            pass
        message = json.dumps(result)
        # print(message)
        message_bytes= bytes(message,encoding='utf8')
        # print(message_bytes)
        self.send_response(200)
        self.end_headers()
        self.wfile.write(message_bytes)
        return




if __name__ == '__main__':
    server = HTTPServer(('localhost',5000),GetHandler)
    server.serve_forever()











